package com.mvetc.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mvetc.service.MovieService;
import com.mvetc.service.impl.MovieServiceImpl;

/**
 * Servlet implementation class movieListServlet
 * @author SunZhiQiang
 * @date 2020-06-21
 */
@WebServlet("/MovieListServlet")
public class MovieListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MovieService movieService = new MovieServiceImpl();
	private static final String OPERATIONKEY = "op";
	private static final String SHOWMOVIESLIST = "showMoviesList";
	private static final String SHOWCOMMENTLIST = "showCommentList";
	private static final String ADDCOMMENT = "addComment";
	private static final String ADDCOLLECT = "addCollect";
	private String movieId;
	private Gson gson = new Gson();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MovieListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#getServletConfig()
	 */
    @Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @SunZhiQiang
	 * @date 2020-06-17
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	movieId = request.getParameter("movieId");
    	String op = request.getParameter(OPERATIONKEY);
    	response.setContentType("application/json;charset=utf-8");
    	
		if(SHOWMOVIESLIST.equals(op)&&movieId!=null) {
			getMovieList(request, response);
		}else if (SHOWCOMMENTLIST.equals(op)&&movieId!=null) {
			getCommentList(request,response);
		}else if (ADDCOMMENT.equals(op)&&movieId!=null) {
			addComment(request,response);
		}else if(ADDCOLLECT.equals(op)&&movieId!=null) {
			addCollect(request,response);
		}
	}
    
    private void addCollect(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
    	String userName = request.getParameter("userName");
		List<Map<String, Object>> userInfoByName = movieService.getUserInfoByName(userName);
		String userId = null;
		for (Map<String, Object> map : userInfoByName) {
			userId = (String) map.get("userId");
		}
		List<Map<String, Object>> collect = movieService.getCollect(userId, movieId);
		if (collect.isEmpty()) {
			int addMovieCollect = movieService.addCollect(userId, movieId);
			if (addMovieCollect>0) {
				response.getWriter().write("addSuccess");
			} else {
				response.getWriter().write("addFail");
			}
		}else {
			int deleteCollect = movieService.deleteCollect(userId, movieId);
			if (deleteCollect>0) {
				response.getWriter().write("deleteSuccess");
			} else {
				response.getWriter().write("deleteFail");
			}
		}
		
		
		
	}

	private void addComment(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
		String userName = request.getParameter("userName");
		String commentContent = request.getParameter("commentContent");
		List<Map<String, Object>> userInfoByName = movieService.getUserInfoByName(userName);
		LocalDateTime localDateTime = LocalDateTime.now();
		String time = localDateTime.toString();
		String datetime = time.replace("T", " ");
		String userId = null;
		for (Map<String, Object> map : userInfoByName) {
			userId = (String) map.get("userId");
		}
		int addComment = movieService.addComment(commentContent, datetime, userId, movieId);
		if (addComment>0) {
			response.getWriter().write("1");
		}else {
			response.getWriter().write("0");
		}
		
		
	}

	private void getCommentList(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
		List<Map<String, Object>> commentsList = movieService.getCommentsByMovieId(movieId);
		String commentListJson = gson.toJson(commentsList);
		response.getWriter().write(commentListJson);
		
	}

	protected void getMovieList(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
    	Map<String, List<Map<String, Object>>> moviesListMap = new HashMap<>(16);
		List<Map<String, Object>> moviesInfoPlay = movieService.getMoviesInfo("movie_id = '"+movieId+"'");
		moviesListMap.put("moviesInfoPlay", moviesInfoPlay);
	
		List<Map<String, Object>> moviesInfoListRight = movieService.getMoviesInfo("order by movie_count limit 8");
		List<Map<String, Object>> moviesInfoListButtom = movieService.getMoviesInfo("order by movie_rating limit 8");
		
		moviesListMap.put("moviesInfoListRight", moviesInfoListRight);
		moviesListMap.put("moviesInfoListButtom", moviesInfoListButtom);
		
		String moviesListJson = gson.toJson(moviesListMap);
		response.getWriter().write(moviesListJson);
	}

}
